//
//  JGAuthenticationController.m
//  JGRider
//
//  Created by 单启志 on 2018/1/30.
//  Copyright © 2018年 ffq. All rights reserved.
//

#import "JGAuthenticationController.h"
#import "JGAuthenticationCell.h"
#import "JGAuthenticationModel.h"
#import "UIButton+LMJ.h"
#import "JGPhotoManager.h"

@interface JGAuthenticationController ()<UITableViewDelegate,UITableViewDataSource,JGAuthenticationCellDelegte>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UIButton *commitBtn;

@property(nonatomic,strong)NSArray<JGAuthenticationModel*> *datas;

//打开相机控制器的集合
@property(nonatomic,strong)NSMutableArray *pickerVcs;
@end

@implementation JGAuthenticationController
static NSString *cellId=@"authenticationCell";
- (void)viewDidLoad {
    [super viewDidLoad];

    [self configSubViews];
}
-(void)configSubViews{
     self.title=@"身份验证";
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.commitBtn];
    

    self.datas=[JGAuthenticationModel models];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
#pragma mark - override
-(UIColor *)lmjNavigationBackgroundColor:(LMJNavigationBar *)navigationBar{

    return [UIColor whiteColor];
}

-(BOOL)lmjNavigationIsHideBottomLine:(LMJNavigationBar *)navigationBar{
    return NO;
}

-(NSMutableAttributedString *)lmjNavigationBarTitle:(LMJNavigationBar *)navigationBar{

    return [self changeTitle:@"身份验证"];
}

-(UIImage *)lmjNavigationBarLeftButtonImage:(UIButton *)leftButton navigationBar:(LMJNavigationBar *)navigationBar{
    return [UIImage imageNamed:@"signup_icon_return"];
}

- (UIStatusBarStyle)navUIBaseViewControllerPreferStatusBarStyle:(LMJNavUIBaseViewController *)navUIBaseViewController
{
    return UIStatusBarStyleDefault;
}

#pragma mark - private
-(NSMutableAttributedString *)changeTitle:(NSString *)curTitle
{
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:curTitle ?: @""];

    [title addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, title.length)];

    [title addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16] range:NSMakeRange(0, title.length)];

    return title;
}

#pragma mark - tableview delegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        return [tableView fd_heightForCellWithIdentifier:cellId configuration:^(JGAuthenticationCell *cell) {
            cell.model=self.datas[indexPath.row];
        }];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    JGAuthenticationCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
    cell.model=self.datas[indexPath.row];
    cell.delegate=self;
    return cell;
}

#pragma mark - cell delegate
//添加图片
-(void)collectionCellDidSelectItemAtIndexPath:(NSIndexPath *)indexPath selectCell:(UITableViewCell *)cell{
    
   NSInteger cellRow=[self.tableView indexPathForCell:cell].row;
    NSInteger selectImageCount=cellRow==0?2:1;
    FQWeakSelf(self);
    //设置cell上次选中的图片
    [[JGPhotoManager share]selectedAssets:self.datas[cellRow].assets];
   [[JGPhotoManager share]photoWithMaxImagesCount:selectImageCount Controller:self callBack:^(NSArray<UIImage *> *photos, NSArray *assets) {
    weakself.datas[cellRow].images=photos.mutableCopy;
        weakself.datas[cellRow].assets=assets.mutableCopy;
       
       [self.tableView reloadData];
       
    }];
    
}
//预览图片
-(void)collectionCellPreviewItemAtIndexPath:(NSIndexPath *)indexPath selectCell:(UITableViewCell *)cell{
    
    NSInteger cellRow=[self.tableView indexPathForCell:cell].row;
    [JGPhotoManager previewWithController:self WithSelectedAssets:self.datas[cellRow].assets selectedPhotos:self.datas[cellRow].images index:indexPath.row];
}
//移除图片
-(void)collectionCellRemoveImageItemAtIndexPath:(NSIndexPath *)indexPath selectCell:(UITableViewCell *)cell{
     NSInteger cellRow=[self.tableView indexPathForCell:cell].row;
    [self.datas[cellRow].images removeObjectAtIndex:indexPath.row-1];
    [self.datas[cellRow].assets removeObjectAtIndex:indexPath.row-1];
    [[JGPhotoManager share]selectedAssets:self.datas[cellRow].assets];
    [self.tableView reloadData];
    
}
#pragma mark - getter setter
-(UITableView *)tableView{

    if (!_tableView) {

        _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0,self.lmj_navgationBar.height, JGScreenW, AdaptedHeight(440-64-10)) style:UITableViewStylePlain];

        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([JGAuthenticationCell class]) bundle:nil] forCellReuseIdentifier:cellId];

        _tableView.showsVerticalScrollIndicator=NO; _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.delegate=self;
        _tableView.dataSource=self;
    }

    return _tableView;

}

-(UIButton *)commitBtn{
    if (!_commitBtn) {
        CGRect frame=CGRectMake((JGScreenW-AdaptedWidth(273))*0.5,AdaptedHeight(440), AdaptedWidth(273),AdaptedHeight(38));

        _commitBtn=[[UIButton alloc]initWithFrame:frame buttonTitle:@"提交" normalBGColor:HEXCOLOR(0xfea129) selectBGColor:HEXCOLOR(0xfea129) normalColor:HEXCOLOR(0xffffff) selectColor:HEXCOLOR(0xffffff) buttonFont:[UIFont systemFontOfSize:15.5] cornerRadius:AdaptedWidth(3) doneBlock:^(UIButton *btn) {

        }];
    }
    return _commitBtn;
}

-(NSMutableArray *)pickerVcs{
    
    if (!_pickerVcs) {
        _pickerVcs=[NSMutableArray array];
    }
    return _pickerVcs;
}

@end
